package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.CharCheckUtil;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/17 0017.
 */

public class SearchLoginPsdActivity extends BaseActivity implements View.OnClickListener{

    private ImageView searchpsd_back;
    private EditText searchpsd_phone_et,searchpsd_code_et,searchpsd_psd_et,searchpsd_psdConfig_et;
    private TextView searchpsd_code_tv,searchpsd_config;
    private ImageView searchpsd_psd_img,searchpsd_psdConfig_img;

    private boolean flag1 = true;
    private boolean flag2 = true;

    private int countSeconds = 60;// 倒数秒数

    private Handler hanlder = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (countSeconds > 0) {
                        --countSeconds;
                        searchpsd_code_tv.setText(countSeconds + "s");
                        hanlder.sendEmptyMessageDelayed(0, 1000);
                    } else {
                        countSeconds = 60;
                        searchpsd_code_tv.setText("重新获取");
                        searchpsd_code_tv.setClickable(true);
                    }
                    break;
            }
        }
    };
    // 开始倒计时
    private void startCountBack() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                searchpsd_code_tv.setText(countSeconds + "s");
                hanlder.sendEmptyMessage(0);
                searchpsd_code_tv.setClickable(false);
            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchpswd);
        initView();
    }

    private void initView(){
        searchpsd_back = (ImageView) findViewById(R.id.searchpsd_back);
        searchpsd_back.setOnClickListener(this);
        searchpsd_phone_et = (EditText) findViewById(R.id.searchpsd_phone_et);
        searchpsd_code_et = (EditText) findViewById(R.id.searchpsd_code_et);
        searchpsd_psd_et = (EditText) findViewById(R.id.searchpsd_phone_psd_et);
        searchpsd_psdConfig_et = (EditText) findViewById(R.id.searchpsd_psdConfig_et);
        searchpsd_code_tv = (TextView) findViewById(R.id.searchpsd_code_tv);
        searchpsd_code_tv.setOnClickListener(this);
        searchpsd_psd_img = (ImageView) findViewById(R.id.searchpsd_psd_img);
        searchpsd_psd_img.setOnClickListener(this);
        searchpsd_psdConfig_img = (ImageView) findViewById(R.id.searchpsd_psdConfig_img);
        searchpsd_psdConfig_img.setOnClickListener(this);
        searchpsd_config = (TextView) findViewById(R.id.searchpsd_config);
        searchpsd_config.setOnClickListener(this);
    }

    /**
     * 找回密码
     */
    private void searchPsd(){
        String phone = searchpsd_phone_et.getText().toString().trim();
        if (phone.isEmpty()){
            showToast("请输入手机号");
            return;
        }
        if (!CharCheckUtil.isPhoneNum(phone)){
            showToast("手机号格式错误");
            return;
        }
        String code = searchpsd_code_et.getText().toString().trim();
        if (code.isEmpty()){
            showToast("请输入验证码");
            return;
        }
        String psd = searchpsd_psd_et.getText().toString();
        if (psd.isEmpty()){
            showToast("请输入密码");
            return;
        }
        if (psd.length()<6){
            showToast("密码不少于6位");
            return;
        }
        String psdConfig = searchpsd_psdConfig_et.getText().toString();
        if (psdConfig.isEmpty()){
            showToast("请再次输入密码");
            return;
        }
        if (!psd.equals(psdConfig)){
            showToast("密码输入不一致");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("phone",phone);
        map.put("code",code);
        map.put("passwd",psd);
        XUtil.Post(UrlConfig.FORGET_LOGINPSD_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("忘记密码result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    SearchLoginPsdActivity.this.finish();
                }
                showToast(jsonObject.getString("msg"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }

    private void getCode(){
        String phone = searchpsd_phone_et.getText().toString().trim();
        if (phone.isEmpty()){
            showToast("请输入手机号");
            return;
        }
        if (!CharCheckUtil.isPhoneNum(phone)){
            showToast("手机号格式错误");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("phone",phone);
        XUtil.Post(UrlConfig.GETCODE_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("获取验证码result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    startCountBack();
                }
                showToast(jsonObject.getString("msg"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("获取验证码失败");
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.searchpsd_back:
                finish();
                break;
            case R.id.searchpsd_code_tv:
                getCode();
                break;
            case R.id.searchpsd_psd_img:
                if (flag1)
                {
                    searchpsd_psd_img.setImageResource(R.mipmap.password_open);
                    searchpsd_psd_et.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    flag1 = false;
                }else {
                    searchpsd_psd_img.setImageResource(R.mipmap.password_close);
                    searchpsd_psd_et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    flag1 = true;
                }
                break;
            case R.id.searchpsd_psdConfig_img:
                if (flag2)
                {
                    searchpsd_psdConfig_img.setImageResource(R.mipmap.password_open);
                    searchpsd_psdConfig_et.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    flag2 = false;
                }else {
                    searchpsd_psdConfig_img.setImageResource(R.mipmap.password_close);
                    searchpsd_psdConfig_et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    flag2 = true;
                }
                break;
            case R.id.searchpsd_config:
                searchPsd();
                break;

        }
    }
}
